//
//  magezineView.m
//  ILiangCang
//
//  Created by ZX on 16-4-18.
//  Copyright (c) 2016年 CRQ. All rights reserved.
//

#import "magezineView.h"
#import "httpEngine.h"
#import "infosModel.h"
#import "topicCell.h"
#import "UIImageView+WebCache.h"
#import "Masonry.h"
#import "MJRefresh.h"
#import "publicWebViewController.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
@implementation magezineView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        _rowArray = [[NSMutableArray alloc]initWithCapacity:0];
        _sectionArray = [[NSMutableArray alloc]initWithCapacity:0];
        [self addTableView];
        _tabView.rowHeight = WIDTH/7*4;
    }

    
    return self;
}

-(void)addTableView
{
    _tabView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-115) ];
    
    _tabView.delegate = self;
    _tabView.dataSource = self;
    [self addSubview:_tabView];
    [_tabView registerClass:[topicCell class] forCellReuseIdentifier:@"cell"];
    
    
    
    [_tabView addHeaderWithTarget:self action:@selector(headerRefreshing)];
    [httpEngine requestToGetMagazineMainCompletion:^(NSMutableArray *sectionArray, NSMutableArray *rowArray, NSError *error) {
        _sectionArray = sectionArray;
        _rowArray = rowArray;
        [_tabView reloadData];
    }];
    
    
}

-(void)headerRefreshing
{
    [httpEngine requestToGetMagazineMainCompletion:^(NSMutableArray *sectionArray, NSMutableArray *rowArray, NSError *error) {
        _sectionArray = sectionArray;
        _rowArray = rowArray;
        [_tabView reloadData];
    }];
    [_tabView headerEndRefreshing];
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _sectionArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //每个区对应的小数组，小数组的个数即row的行数
    NSArray *array=_rowArray[section];
    return array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    topicCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    NSArray *array=_rowArray[indexPath.section];//找到小数组
    infosModel * info = array[indexPath.row];//小数组内部的对象
    [cell.coverImage sd_setImageWithURL:info.coverImage];
    cell.titleLabel.text = info.topicName;
    cell.classLabel.text = [NSString stringWithFormat:@"#%@ • 作者:%@",info.catName,info.authorName];
    
    
    
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 35;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 35)];
//    UIImageView * bg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 35)];
//    bg.image = [UIImage imageNamed:@"nav_bar"];
//    [view addSubview:bg];

    view.backgroundColor = [UIColor colorWithRed:0.0/255 green:0.0/255 blue:0.0/255 alpha:0.8];
    
    UIImageView * banner = [[UIImageView alloc]init];
    banner.image = [UIImage imageNamed:@"sp_line"];
    [view addSubview:banner];
    [banner mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(view.mas_centerX);
        make.centerY.mas_equalTo(view.mas_centerY);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(5);
        
    }];
    
    static NSString * dataStr ;
    if (section == 0) {
        dataStr = [_sectionArray[section] substringWithRange:NSMakeRange(5, 5)];
    }else
    {
        dataStr = [_sectionArray[section] substringWithRange:NSMakeRange(5, 6)];
    }
    UILabel * label = [[UILabel alloc]init];
    label.text = dataStr;
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:13];
    [view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(view.mas_centerX);
        make.centerY.mas_equalTo(view.mas_centerY);
    }];
    
    
    return view;
}

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.delegate respondsToSelector:@selector(magazineCellDidSelectedSection:Row:with:and:)]) {
        [self.delegate magazineCellDidSelectedSection:indexPath.section Row:indexPath.row with:_sectionArray and:_rowArray];
    }
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat sectionHeaderHeight = 35;
    if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0)
    {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
        
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight)
    {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        
    }
    
}



@end
